const Mock = require('mockjs')
const { list: listMovieLevels, find: findMovieLevel } = require('./movie-level')
const { find: findMovie } = require('./movie')
const { list: listOrgUnit, find: findOrgUnit } = require('./org-units')

const movieLevels = listMovieLevels(null)
const orgUnits = listOrgUnit(1, 50, 1).data.records
const Movies = []
let id = 1
for (let i = 0; i < 26; ++i) {
  const minP = (i % 5 + 1)
  Movies.push(Mock.mock({
    id: id++,
    orgUnitId: orgUnits[i % orgUnits.length].id,
    movieId: id,
    movieName: '店铺剧本' + i,
    author: Mock.Random.cname(),
    cover: '',
    movieVersion: '1.0.1',
    tag: ['剧情', '恐怖'],
    brief: '简单介绍一下',
    minPlayers: minP,
    maxPlayers: minP + (i % 6 + 3),
    publishTime: Mock.Random.datetime(),
    playTimes: Mock.Random.integer(1, 100),
    duration: Mock.Random.float(1, 10, 0.1, 0.5),
    status: Mock.Random.integer(0, 1),
    movieLevelId: movieLevels.data[i % movieLevels.data.length].id,
    price: movieLevels.data[i % movieLevels.data.length].price
  }))
}

function list(page, limit, name, author, tag, minPlayers, maxPlayers, minDuration, maxDuration, orgUnitId) {
  limit = Math.max(1, Math.min(parseInt(limit), 50))
  page = Math.max(parseInt(page), 1)
  minPlayers = minPlayers ? parseInt(minPlayers) : null
  maxPlayers = maxPlayers ? parseInt(maxPlayers) : null
  minDuration = minDuration ? parseInt(minDuration) : null
  maxDuration = maxDuration ? parseInt(maxDuration) : null
  orgUnitId = orgUnitId ? parseInt(orgUnitId) : null
  let mocks = Movies.filter(item => {
    if (minPlayers && item.minPlayers < minPlayers && item.maxPlayers < minPlayers) return false
    if (maxPlayers && item.minPlayers > maxPlayers && item.maxPlayers > maxPlayers) return false
    if (minDuration && item.duration < minDuration) return false
    if (maxDuration && item.duration > maxDuration) return false
    if (author && !item.author && item.author.indexOf(author) === -1) return false
    if (tag && item.tag && item.tag.indexOf(tag) === -1) return false
    if (orgUnitId && item.orgUnitId !== orgUnitId) return false
    return !(name && (!item.name || item.name.indexOf(name) === -1))
  })
  const total = mocks.length
  mocks = mocks.filter((u, index) => index < limit * page && index >= limit * (page - 1))
  mocks = mocks.map(item => {
    const lvl = findMovieLevel(item.movieLevelId)
    if (lvl) {
      item.movieLevelName = lvl.name
    }
    const orgUnit = findOrgUnit(item.orgUnitId)
    if (orgUnit) {
      item.orgUnitName = orgUnit.name
    }
    return item
  })
  return { success: true, data: { total: total, records: mocks }}
}

function find(id) {
  id = id ? parseInt(id) : null
  for (let i = 0; i < Movies.length; ++i) {
    if (Movies[i].id === id) {
      return Movies[i]
    }
  }
  return null
}

function add(movie) {
  const r = JSON.parse(JSON.stringify(movie))
  const price = r.price
  const m = findMovie(movie.movieId)
  const item = Object.assign(r, m)
  item.price = price
  item.id = Movies[Movies.length - 1].id + 1
  console.log('item.price=' + item.price)
  Movies.push(item)
}

module.exports = { list, find, add }
